﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AdmLaboratorios.DAO;
using AdmLaboratorios.DTO;
using System.Data.SqlClient;

namespace AdmLaboratorios.Business
{
    public static class Partners
    {
        static PartnerDAO daoPartner = DAOFactory.Get<PartnerDAO>();

        public static List<PartnerDTO> PartnersFiltered(string filtro)
        {
            string where = string.Format("Nombre LIKE '%{0}%' OR Tipo LIKE '%{0}%'", filtro);
            return daoPartner.ReadAll(where);
        }


        public static PartnerDTO Crear(PartnerDTO partner)
        {
            using (SqlTransaction tran = daoPartner.IniciarTransaccion())
            {
                partner = daoPartner.Create(partner, tran);
                
                daoPartner.Log(LogDTO.eAcciones.Alta, "Crear", partner.Id, tran);
                daoPartner.CommitTransaccion(tran);

                return partner;
            }

            //SqlTransaction tran = null;
            //try
            //{
            //    tran = daoPartner.IniciarTransaccion();

            //    partner = daoPartner.Create(partner, tran);

            //    daoPartner.Log(LogDTO.eAcciones.Alta, "Crear", partner.Id, tran);

            //    daoPartner.CommitTransaccion(tran);

            //    return partner;
            //}
            //catch (Exception)
            //{
            //    daoPartner.RollbackTransaccion(tran);
            //    throw;
            //}
        }

        public static PartnerDTO Get(int id)
        {
            return daoPartner.Read(id);
        }

        public static void Modificar(PartnerDTO partner)
        {
            using (SqlTransaction tran = daoPartner.IniciarTransaccion())
            {
                daoPartner.Update(partner, partner.Id, tran);

                daoPartner.Log(LogDTO.eAcciones.Modificacion, "Modificación", partner.Id, tran);

                daoPartner.CommitTransaccion(tran);
            }
        }

        public static bool Eliminar(int id)
        {
            try
            {
                using (SqlTransaction tran = daoPartner.IniciarTransaccion())
                {
                    daoPartner.Delete(id, tran);
                    daoPartner.Log(LogDTO.eAcciones.Baja, "Eliminación de Producto", id, tran);

                    daoPartner.CommitTransaccion(tran);

                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }


            //using (SqlTransaction tran = daoPartner.IniciarTransaccion())
            //{
            //    daoPartner.Delete(partner.Id, tran);

            //    daoPartner.Log(LogDTO.eAcciones.Baja, "Baja", partner.Id, tran);
            //    daoPartner.CommitTransaccion(tran);
            //}
        }

        //public static void Eliminar(int id)
        //{
        //    using (SqlTransaction tran = daoPartner.IniciarTransaccion())
        //    {
        //        daoPartner.Delete(id, tran);
        //    }
        //}

        public static object Todos()
        {
            throw new NotImplementedException();
        }
    }
}
